package com.ym.controller.patient.findDoctor;

import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.core.utils.tkmybatis.TKCriteria;
import com.ym.medical.domain.entity.EdBodyGuidEntity;
import com.ym.medical.domain.vo.DeptRelationAndNumVO;
import com.ym.medical.service.IEdBodyGuidSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "患者端-身体部位找医生")
@RequestMapping(value = "/patient/find/position",headers= Constants.Api.VERSION)
@RestController
@ApiSign
@Validated
public class PositionFindController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(PositionFindController.class);

	@Autowired
	private IEdBodyGuidSV edBodyGuidSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("部位对应病症相关数据获取")
	@GetMapping(value = "/data")
	public APIResponse<List<EdBodyGuidEntity>> positionData(
			@RequestParam @Valid @Pattern(regexp = "男性|女性") String sex,
			String position,
			String symptom,
			Long id
	) {
		TK<EdBodyGuidEntity> select = TK.select(EdBodyGuidEntity.class);
		TKCriteria<EdBodyGuidEntity, Object> where = select.where();
		where.andEqualTo(EdBodyGuidEntity::getSex,sex);
		if (id != null) {
			//ID直接查找定位某一条具体记录
			where.andEqualTo(EdBodyGuidEntity::getId,id).end();
		} else if (StringUtils.isBlank(symptom) && StringUtils.isNotBlank(position)) {
			// 查找部位相关的症状
			select.distinct().column("symptom").orderBy(EdBodyGuidEntity::getSortId);
			where.andEqualTo(EdBodyGuidEntity::getPosition,position);
		} else if (StringUtils.isNotBlank(symptom) && StringUtils.isNotBlank(position)) {
			// 查询不问相关症状的疾病描述列表
			where.andEqualTo(EdBodyGuidEntity::getPosition,position)
					.andEqualTo(EdBodyGuidEntity::getSymptom,symptom);
			select.distinct().column("id", "symptom","department","content", "reason", "diagnosis", "treat").orderBy(EdBodyGuidEntity::getSortId);
		} else {
			select.distinct().column("position","icon").orderBy(EdBodyGuidEntity::getSortId);
			where.andIsNotNull(EdBodyGuidEntity::getIcon).andEqualTo(EdBodyGuidEntity::getSex,sex);
		}
		List<EdBodyGuidEntity> data = edBodyGuidSV.selectList(select);
		return successData(data);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("获取科室对应的ID以及医生人数")
	@GetMapping(value = "/deptRelationAndNum")
	public APIResponse<Map<String, DeptRelationAndNumVO>> deptRelationAndNum() {
		List<DeptRelationAndNumVO> deptRelationAndNumVO = edBodyGuidSV.deptRelationAndNum();
		Map<String, DeptRelationAndNumVO> collect = deptRelationAndNumVO.stream().collect(Collectors.toMap(DeptRelationAndNumVO::getDeptName, deptVo -> deptVo));
		return successData(collect);
	}

}
